package main

import (
	"crypto/tls"
	"fmt"
	"time"
)

// 思考： config 模式和 builder 模式的区别点
type Server struct {
	Addr     string
	Port     int
	Protocol string
	Timeout  time.Duration
	MaxConns int
	TLS      *tls.Config
}

type ServerBuilder struct {
	Server
}

func (sb *ServerBuilder) Create(addr string, port int) *ServerBuilder {
	sb.Server.Addr = addr
	sb.Server.Port = port
	return sb
}

func (sb *ServerBuilder) WithProtocol(protocol string) *ServerBuilder {
	sb.Server.Protocol = protocol
	return sb
}

func (sb *ServerBuilder) WithMaxConn(macconn int) *ServerBuilder {
	sb.MaxConns = macconn
	return sb
}

func (sb *ServerBuilder) WithTimeout(timeout time.Duration) *ServerBuilder {
	sb.Timeout = timeout
	return *sb
}

func (sb *ServerBuilder) WithTLS(tls *tls.Config) *ServerBuilder {
	sb.Server.TLS = tls
	return sb
}

func (sb *ServerBuilder) Build() Server {
	return sb.Server
}

func main() {
	sb := ServerBuilder{}
	server := sb.Create("127.0.0.1", 8080).WithProtocol("upd").WithMaxConn(1024).WithTimeout(30 * time.Second).Build()
	fmt.Println(server)
}
